package com.design.softwaredesign.service.impl;

import com.design.softwaredesign.entity.GoodCategory;
import com.design.softwaredesign.mapper.BackStatisticsMapper;
import com.design.softwaredesign.service.BackStatisticsService;
import com.design.softwaredesign.utils.model.PanelGroupDataModel;
import com.design.softwaredesign.utils.model.PieChartDataModel;
import com.design.softwaredesign.vo.BarVo;
import com.design.softwaredesign.vo.StatisticsVo;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

//import static javafx.scene.input.KeyCode.T;

/**
 * @author :WaiterXiaoYY
 * @description: TODO
 * @data :2020/11/3 18:40
 */
@Service
public class BackStatisticsServiceImpl implements BackStatisticsService {
    @Autowired
    private BackStatisticsMapper backStatisticsMapper;
    @Override
    public StatisticsVo getPanelGroupData() {
        StatisticsVo statisticsVo = new StatisticsVo();
        statisticsVo.setCode(202);
        statisticsVo.setMsg("获取数据失败");
        try {
            HashMap<String, PanelGroupDataModel> panelGroupDataModelHashMap = new HashMap<>();
            PanelGroupDataModel panelGroupDataModel = new PanelGroupDataModel();
            panelGroupDataModel.setUser(backStatisticsMapper.countAllUser());
            panelGroupDataModel.setTraded(backStatisticsMapper.countGoodByState("已出售"));
            panelGroupDataModel.setUntraded(backStatisticsMapper.countGoodByState("待出售"));
            panelGroupDataModel.setDonation(backStatisticsMapper.countAllDonation());
            panelGroupDataModelHashMap.put("panelgroup", panelGroupDataModel);
            statisticsVo.setData(panelGroupDataModelHashMap);
            statisticsVo.setCode(200);
            statisticsVo.setMsg("获取数据成功");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return statisticsVo;
        }
    }

    @Override
    public StatisticsVo getSexChartData() {

        StatisticsVo statisticsVo = new StatisticsVo();
        statisticsVo.setCode(202);
        statisticsVo.setMsg("获取数据失败");
        try {
            List<PieChartDataModel> pieChartDataModelList = new ArrayList<>();
            HashMap<String, List> listHashMap = new HashMap<>();
            PieChartDataModel pieChartDataModel1 = new PieChartDataModel("男性", backStatisticsMapper.countUserSex("男"));
            PieChartDataModel pieChartDataModel2 = new PieChartDataModel("女性", backStatisticsMapper.countUserSex("女"));
            PieChartDataModel pieChartDataModel3 = new PieChartDataModel("保密",backStatisticsMapper.countUserSex("保密"));
            pieChartDataModelList.add(pieChartDataModel1);
            pieChartDataModelList.add(pieChartDataModel2);
            pieChartDataModelList.add(pieChartDataModel3);

            listHashMap.put("sexchart", pieChartDataModelList);
            statisticsVo.setData(listHashMap);
            statisticsVo.setCode(200);
            statisticsVo.setMsg("获取数据成功");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return statisticsVo;
        }
    }

    @Override
    public BarVo getBarChartData() {
        BarVo barVo = new BarVo();
        barVo.setCode(202);
        barVo.setMsg("获取数据失败");
        try {
            List<GoodCategory> goodCategoryList = null;
            goodCategoryList = backStatisticsMapper.getGoodCategory();

            HashMap<String, Integer[]> barHashMap = new HashMap<>();

            Integer[] traded = new Integer[goodCategoryList.size()];
            Integer[] untraded = new Integer[goodCategoryList.size()];
            String[] category = new String[goodCategoryList.size()];

            for(int i = 0; i < goodCategoryList.size(); i++) {
                category[i] = goodCategoryList.get(i).getGoodCategory();
                traded[i] = backStatisticsMapper.countGoodNumByCategoryIdAndState(goodCategoryList.get(i).getGoodCategoryId(), "已出售");
                untraded[i] = backStatisticsMapper.countGoodNumByCategoryIdAndState(goodCategoryList.get(i).getGoodCategoryId(), "待出售");
            }

            barHashMap.put("untraded", untraded);
            barHashMap.put("traded", traded);

            barVo.setCategory(category);
            barVo.setData(barHashMap);
            barVo.setCode(200);
            barVo.setMsg("获取数据成功");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return barVo;
        }
    }
}
